$(document).ready(function() {	
	function accord(){
		$("#accordion").accordion({
			autoHeight: false,
			navigation: true
		});
	};
	
	/**
	 * 选择角色时的添加角色动作
	 * @memberOf {TypeName} 
	 */
	
	$("#addDept").click(function() {
		if ($("#allDept option:selected").length > 0) {
			$("#allDept option:selected").each(function() {
				$("#selDept").append("<option class='class1' name='selDept' selected value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#delDept").click(function() {
		if ($("#selDept option:selected").length > 0) {
			$("#selDept option:selected").each(function() {
				$("#allDept").append("<option class='class1' value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#addLeadDept").click(function() {
		if ($("#selDept option:selected").length > 0) {
			$("#selDept option:selected").each(function() {
				$("#leadDept").append("<option class='class1' name='leadDept' selected value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#delLeadDept").click(function() {
		if ($("#leadDept option:selected").length > 0) {
			$("#leadDept option:selected").each(function() {
				$("#selDept").append("<option class='class1' value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	$("#addRole").click(function() {
		if ($("#allRole option:selected").length > 0) {
			$("#allRole option:selected").each(function() {
				$("#selRole").append("<option class='class1' name='selRole' selected value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#delRole").click(function() {
		if ($("#selRole option:selected").length > 0) {
			$("#selRole option:selected").each(function() {
				$("#allRole").append("<option class='class1' value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#dialog").dialog("destroy");
		
	var uId = $("#uId"),
		uName = $("#uName"),
		uCId = $(".CId"),
		uPassword = $("#uPassword"),
		pwd_check = $("#pwd_check"),
		uState = $("#selUState"),
		uRole = $("#uRole"),
		uDept = $("#uDept"),
		allFields = $([]).add(uId).add(uName).add(uPassword).add(pwd_check).add(uState).add(uRole).add(uDept) 
	
	$("#dialog-form").dialog({
		autoOpen: false,
		position:'top',
		maxHeight:600,
		width: 460,
		modal: true,
		//resizable: false,
	    close: function() {
			allFields.val('');
			$("#accordion").accordion( "disable" );
			$("#accordion").remove();
			$("#user-form input").removeClass('invalid');
			$("#user-form label").remove('.invalid');
	 	}
	});
	
	$("#cancel").click(function(){
 
		$("#dialog-form").dialog("close");
	});	
	
	$('#create-user')
		.button()
		.click(function() {
			$('#dialog-form').dialog('open');
	});
	
	/**
	 * 移除焦点获取用户表名字相同的通讯录
	 */
	uName.focusout(function() {
  		$.ajax({
   			type: "post",
   			data: {"user.UName":uName.val()},
   			url: "admin/user_findContactByUName.action",
   			success:function(data){
				if(data.ajaxRst=="success"){
					$("#accordion").remove();
					$("#ucname").after("<div id='accordion' style='width:328;'></div>");
					$.each(data.contacts,function(i,n){
						fillToHtml(n.CId, n.CName, n.tbDept.DName,n.CTel, n.CCellphone, n.CFax, n.CVideo );
					});
					$("#accordion").css("display","block");
					accord();
				}	
   			}
  		});
	});
	$("#userForm").validate({
		//onsubmit: false,
		//onkeyup: false,
		errorClass: "invalid",
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form  
			var cid = $(".CId").val();
			var sel = $("#selRole option");
			if(sel.length==0){
				$("#message").text("角色一定要选择！").css("color","red");
				showMessage();
				return;
			}
			var roles = new Array(sel.length);
			for(var i = 0;i < sel.length; i++ ){
				var value=$(sel[i]).val();
				roles.push(value);
			}
       		$.ajax( {
				type : "post",
				data : {"user.UId":uId.val(),"user.UName":uName.val(),
       					"user.tbContact.CId":cid,"user.UPassword":uPassword.val(),
       					"selRoles":roles.toString(),"user.tbUserState.usId":uState.val(),
       					"user.tbDept.DId":uDept.val()
						 },
				url : "admin/user_save.action",
				success : function(data){
					 $("#message").text("成功！").css("color","red");
					 showMessage();
					 firstLoadUser();
				}
			});
			$("#dialog-form").dialog('close');
    	}, 
		rules: {
			'user.UId':{
    			required:true,
    			remote:{
    				url: "admin/user_checkUId.action",
   					type: "post"
    			}
    		},
			'user.UName':{
    			required:true,
    			minlength: 2
    		},
			'user.UPassword':'required',
			'pwd_check': {  
            	required: true,  
            	equalTo: "#uPassword"  
       		 },
       		 'selRole':{
       			 minlength:1
       		 }
		},
		messages: {
		'user.UId':{
			required:'必须填写',
			remote:'已经被占用'
		},
		'user.UName':{
			required:'必须填写',
			minlength:'不小于两个字',
			remote:'remote'
		},
		'user.UPassword':"必须填写",
		'pwd_check':"密码要一致",
		'selRole':"*"
		}
	});
	
	function fillToHtml(CId, CName, DName,Tel, Cellphone, Fax, Video){
		var strHtml = "<h3><a>"+CId+"</a></h3>";	
			strHtml += "<div><ul>";
			strHtml += "<li>"+"所属部门："+DName+"</li> ";
			strHtml += "<li>"+"电话："+Tel+"</li> ";
			strHtml += "<li>"+"手机："+Cellphone+"</li> ";
			strHtml += "<li>"+"传真："+Fax+"</li> ";
			strHtml += "<li>"+"视频："+Video+"</li> ";
			strHtml += "<input type='radio' class='CId' name='CId' value='"+CId+"'/>确定这是你的通讯录";
			strHtml += "</ul></div>";
			$("#accordion").append(strHtml);
	}
});	